package com.jiaying.patient.dao;

import com.jiaying.patient.domain.Doctor;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 访问医生表的数据访问对象： Dao
 */
public class DoctorDao {

    //public Doctor selectDoctor(String name,String password);

    public Doctor selectDoctor(Doctor doctor){
        //创建连接对象 Connection
        Connection connection=null;
        //创建执行sql语句的对象：PreparedStatement
        PreparedStatement stmt=null;
        //创建接收查询结果的对象：Resultset
        ResultSet rs=null;

        Doctor doctor_result=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/his_jiaying?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai","root","123456");
            String sql="select col_doctor_name,col_doctor_password from tab_doctor where col_doctor_name=? and col_doctor_password=?";
            stmt=connection.prepareStatement(sql);
            stmt.setString(1,doctor.getDoctor_name());
            stmt.setString(2,doctor.getDoctor_password());

            rs=stmt.executeQuery();

            //因为这个结果只有一条数据，所有我们用一个Doctor对象来接收查询的结果

            if(rs.next()){  //指针下滑一行，指向首条数据
                    //进入这里，代表查询结果中有数据
                doctor_result=new Doctor();
                doctor_result.setDoctor_name(rs.getString("col_doctor_name"));
                doctor_result.setDoctor_password(rs.getString("col_doctor_password"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doctor_result;
    }

    public static void main(String[] args) {
        Doctor d=new Doctor("孙思邈","123456");
        Doctor query=new DoctorDao().selectDoctor(d);

        System.out.println("查询的结果："+query.getDoctor_name());
    }
}
